package com.uifp.gradebook;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import com.uifp.gradebook.adaptors.ExpandableClassesAdaptor;
import com.uifp.gradebook.business_objects.SemesterChild;
import com.uifp.gradebook.business_objects.SemesterParent;

import android.os.Bundle;
import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.support.v4.app.DialogFragment;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;

public class MainActivity  extends Activity {
	 
		private ExpandableListAdapter listAdapter;
	    private ExpandableListView expListView;
	    private List<String> listDataHeader;
	    private HashMap<String, List<SemesterChild>> listDataChild;
	    private DataBaseHelper dbHelper;
		private ActionBar actionBar;
		public final static String CLASS_ID = "com.uifp.CLASS";
		
	    public MainActivity(){
	    
	    }
	 
	    @Override
	    protected void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        setContentView(R.layout.expandable_activity);
	        
	        this.actionBar = getActionBar();
	        actionBar.setTitle("Dashboard");
	        
	        this.dbHelper = new DataBaseHelper(this);
	        
	        //create the database the first time the app is launched
	     	try {
				dbHelper.createDataBase();
			} catch (IOException e) {
				e.printStackTrace();
			} 
	        
	        // get the listview
	        expListView = (ExpandableListView) findViewById(R.id.lvExp);
	        
	    }
	    public void onResume(){
	        super.onResume();
	        // preparing list data
	        prepareListData();
	 
	        listAdapter = new ExpandableClassesAdaptor(this, listDataHeader, listDataChild);
	 
	        // setting list adapter
	        expListView.setAdapter(listAdapter);
	 
	        // Listview Group click listener
	        expListView.setOnGroupClickListener(new OnGroupClickListener() {
	 
	            @Override
	            public boolean onGroupClick(ExpandableListView parent, View v,
	                    int groupPosition, long id) {
	                // Toast.makeText(getApplicationContext(),
	                // "Group Clicked " + listDataHeader.get(groupPosition),
	                // Toast.LENGTH_SHORT).show();
	                return false;
	            }
	        });
	 
	        // Listview on child click listener
	        expListView.setOnChildClickListener(new OnChildClickListener() {
	 
	            @Override
	            public boolean onChildClick(ExpandableListView parent, View v,
	                    int groupPosition, int childPosition, long id) {

	             	int classID = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getClassId();
	            	Intent intent = new Intent(parent.getContext(), ViewSelectedClassActivity.class);
	            	intent.putExtra(CLASS_ID, classID);
	            	startActivity(intent);
	                return false;
	            }
	        });
	        expListView.setOnItemLongClickListener(new OnItemLongClickListener() {
	            @Override
	            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
	                if (ExpandableListView.getPackedPositionType(id) == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
	                    int groupPosition = ExpandableListView.getPackedPositionGroup(id);
	                    int childPosition = ExpandableListView.getPackedPositionChild(id);
		             	int classID = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition).getClassId();
	                    dbHelper.removeClass(classID);
	                    onResume();
	                    return true;
	                }
	                return false;
	            }
	        });
	       
	    }

	    /*
	     * Preparing the list data
	     */
	   private void prepareListData() {
	    	
	    	listDataHeader = new ArrayList<String>();
	    	ArrayList<SemesterParent> listSemester = dbHelper.getEnrolledSemesters(this);
	    	listDataChild = new HashMap<String, List<SemesterChild>>();
	    	Iterator<SemesterParent> iParent = listSemester.iterator();
	    	while(iParent.hasNext()){
	    		SemesterParent parent = iParent.next();
	    		String className = parent.getLabel();
	    		String semester = parent.getSemester();
	    		String yearString = parent.getYearString();
	    		listDataHeader.add(className);
	    		ArrayList<SemesterChild> listChild = dbHelper.getSemesterClasses(this, semester, yearString);
	    		listDataChild.put(className, listChild);
	    	}
	    }
	   @Override
	    public boolean onCreateOptionsMenu(Menu menu) {
	        // Inflate the menu items for use in the action bar
	        MenuInflater inflater = getMenuInflater();
	        inflater.inflate(R.menu.main_activity_actions, menu);
	        return super.onCreateOptionsMenu(menu);
	    }
		@Override
		public boolean onOptionsItemSelected(MenuItem item) {
		    // Handle presses on the action bar items
		    switch (item.getItemId()) {
		        case R.id.action_help:
		        	DialogFragment newFragment = new HelpDialogFragment();
//		            newFragment.show(getFragmentManager(), "Help Test");
		            return true;
		        case R.id.action_new:
		        	Intent myIntent = new Intent(MainActivity.this, SearchClassActivity.class);
		        	MainActivity.this.startActivity(myIntent);
		            return true;
		        default:
		            return super.onOptionsItemSelected(item);
		    }
		}
	}